
package com.hyst.seviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyst.domain.ProgressReport;
import com.hyst.repository.ProgressReportMapper;
import com.hyst.repository.TestReportMapper;
import com.hyst.service.ProgressReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Description:
 * @Date：2018年09月08日 
 * @author： dy 
 * @version ：1.0 1.[2018年09月08日]
 */
@Service
public class ProgressReportServiceImpl implements ProgressReportService {

	@Autowired
	private ProgressReportMapper mapper;


	@Autowired
	private TestReportMapper testReportMapper;

	@Override
	public Page<ProgressReport> list(final ProgressReport progressReport) {
		final Page<ProgressReport> page = PageHelper.startPage(progressReport.getPageNum(), progressReport.getPageSize());
		mapper.select(progressReport);
		return page;
	}

	@Override
	public ProgressReport insert(final ProgressReport progressReport) {
		try {
			if (null != progressReport.getPurchaseCourseId()) {
				//通过购买课程记录查询教材ID
				Map map = testReportMapper.getMaterialById(progressReport.getPurchaseCourseId());
				if (map != null && map.containsKey("material") && StringUtils.isNotBlank((map.get("material").toString()))) {
					progressReport.setMaterialId((map.get("material").toString()));
				}
				if (map != null && map.containsKey("hours_enrolled") && StringUtils.isNotBlank(map.get("hours_enrolled").toString())) {
//				Integer minutes = (Integer) map.get("hours_enrolled");
//				DecimalFormat df = new DecimalFormat("0.0");
					progressReport.setEnrolledHours((Integer) map.get("hours_enrolled"));
//				progressReport.setEnrolledHoursStr(df.format((float) (minutes) / 60));
				}
				if (map != null && map.containsKey("hours_remaining") && StringUtils.isNotBlank(map.get("hours_remaining").toString())) {
//				Integer minutes = (Integer) map.get("hours_remaining");
//				DecimalFormat df = new DecimalFormat("0.0");
					progressReport.setRemainingHours((Integer) map.get("hours_remaining"));
//				progressReport.setEnrolledHoursStr(df.format((float) (minutes) / 60));
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		mapper.insertSelective(progressReport);
		return progressReport;
	}

	@Override
	public ProgressReport getByPriKey(final Integer id) {
		final ProgressReport progressReport = mapper.selectByPrimaryKey(id);
		return progressReport;
	}

	@Override
	public ProgressReport updateSelective(final ProgressReport progressReport) {
		progressReport.setMaterialId(null);
		mapper.updateByPrimaryKeySelective(progressReport);
		return progressReport;
	}

	@Override
	public ProgressReport update(final ProgressReport progressReport) {
		mapper.updateByPrimaryKey(progressReport);
		return progressReport;
	}

	@Override
	public ProgressReport delete(final Integer id) {

		return null;
	}

	@Override
	public String getPcUid(Integer progressReportId) {
		return mapper.getPcUid(progressReportId);
	}

}
